package com.youzi.blue.utils;


import android.annotation.SuppressLint;
import android.content.Context;

import com.youzi.blue.R;
import com.youzi.blue.config.BlueConfig;

import java.io.IOException;
import java.util.Properties;

import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.ResponseBody;

public class OkHttp {
    private final Request.Builder requestBuilder;
    private final OkHttpClient okHttpClient;
    private final LoggerFactory log = LoggerFactory.getLogger();

    private OkHttp() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        okHttpClient = builder.addInterceptor(new RequestLoggerInterceptor())
                .addInterceptor(new ResponseLoggerInterceptor())
                .build();
        requestBuilder = new Request.Builder();//省的每次都new  request操作,直接builder出来,随后需要什么往里加,build出来即可
    }

    private static final class OkHttpHolder {
        static final OkHttp okHttp = new OkHttp();
    }

    public static OkHttp getInstance() {
        return OkHttpHolder.okHttp;
    }


    public void httpGet(String uri, final Callback callback) {
        String url = forMatUrl(uri);

        Request request = requestBuilder.url(url).get().build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    public void httpPost(String uri, FormBody formBody, Callback callback) {
        String url = forMatUrl(uri);

        Request request = requestBuilder.url(url).method("POST", formBody).build();
        okHttpClient.newCall(request).enqueue(callback);
    }

    public String httpGet(String uri) throws IOException {
        String url = forMatUrl(uri);
        Request request = requestBuilder.url(url).get().build();
        Response response = okHttpClient.newCall(request).execute();
        return new String(response.body().bytes());
    }

    public String httpPost(String uri, FormBody formBody) throws IOException {
        String url = forMatUrl(uri);
        Request request = requestBuilder.url(url).method("POST", formBody).build();
        Response response = okHttpClient.newCall(request).execute();
        return new String(response.body().bytes());
    }

    private String forMatUrl(String uri) {
        String url = "http://" + BlueConfig.serverIp + ":" + BlueConfig.serverHttpPort;
        if (uri.startsWith("/")) {
            url += uri;
        } else {
            url += "/" + uri;
        }
        return url;
    }

    /**
     * 请求拦截器
     */
    class RequestLoggerInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();

            log.info("url    =  : " + request.url());
            log.info("method =  : " + request.method());
            log.info("headers=  : " + request.headers());
            log.info("body   =  : " + request.body());

            return chain.proceed(request);
        }
    }

    /**
     * 响应拦截器
     */
    class ResponseLoggerInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());

            log.info("code    =  : " + response.code());
            log.info("message =  : " + response.message());
            log.info("protocol=  : " + response.protocol());

            if (response.body() != null && response.body().contentType() != null) {
                MediaType mediaType = response.body().contentType();
                String string = response.body().string();
                log.info("mediaType=  :  " + mediaType.toString());
                log.info("string   =  : " + string);
                ResponseBody responseBody = ResponseBody.create(mediaType, string);
                return response.newBuilder().body(responseBody).build();
            } else {
                return response;
            }
        }
    }
}